#define SPI_CS    16
#define SPI_CLK   17
#define SPI_MOSI  18
#define SPI_MISO  35
#define SPI_DELAY 2  
void InitSPI(void)
{
  pinMode(SPI_CS,OUTPUT);
  pinMode(SPI_CLK,OUTPUT);
  pinMode(SPI_MOSI,OUTPUT);
  pinMode(SPI_MISO,INPUT_PULLUP );
  
  
}
void SPISendByte(uint8 send_byte)
{
  uint32 i;
  for(i=0;i<8;i++)
  {
    
     if ((send_byte & 0x80)) 
            digitalWrite(SPI_MOSI,HIGH);			 
     else  digitalWrite(SPI_MOSI,LOW);
     send_byte<<=1;
      digitalWrite(SPI_CLK,LOW);
     delayMicroseconds(SPI_DELAY);
     digitalWrite(SPI_CLK,HIGH);
     delayMicroseconds(SPI_DELAY);
  }
   digitalWrite(SPI_CLK,LOW);
   
}
uint8 SPIReadByte(void)
{
  uint32 i;
  uint8 result;
    result=0;
   digitalWrite(SPI_CLK,LOW);
    delayMicroseconds(SPI_DELAY);
   for(i=0;i<8;i++)
   {
     result=result<<1;
     digitalWrite(SPI_CLK,HIGH);
     delayMicroseconds(SPI_DELAY);
     if(digitalRead(SPI_MISO))
       result+=1; 
      
      digitalWrite(SPI_CLK,LOW);
      delayMicroseconds(SPI_DELAY);
   }
  return result;
}
  
void SI4432Write(uint8 addr,uint8 data)
{
    digitalWrite(SPI_CS,HIGH);
    digitalWrite(SPI_CLK,LOW);
    digitalWrite(SPI_CS,LOW);
    
  addr|=0x80;
  SPISendByte(addr);
  SPISendByte(data);
  digitalWrite(SPI_CS,HIGH);
}
uint8 SI4432Read(uint8 addr)
{
   uint8 result;
    digitalWrite(SPI_CS,HIGH);
    digitalWrite(SPI_CLK,LOW);
    digitalWrite(SPI_CS,LOW);
   SPISendByte(addr&0x7f);
   result=SPIReadByte();
   digitalWrite(SPI_CS,HIGH);
   return result;
}
void SI4432MultiWrite(uint8 startAddr,uint8 *ptr,uint8 num)
{ 
    uint32 i;
    digitalWrite(SPI_CS,HIGH);
    digitalWrite(SPI_CLK,LOW);
    digitalWrite(SPI_CS,LOW);
    
  startAddr|=0x80;
  SPISendByte(startAddr);
  for(i=0;i<num;i++)
  SPISendByte(ptr[i]);
  digitalWrite(SPI_CS,HIGH);
}
void SI4432MultiRead(uint8 startAddr,uint8 *ptr,uint8 num)
{
 uint32 i;
    digitalWrite(SPI_CS,HIGH);
    digitalWrite(SPI_CLK,LOW);
    digitalWrite(SPI_CS,LOW);
   SPISendByte(startAddr&0x7f);
   for(i=0;i<num;i++)
   ptr[i]=SPIReadByte();
   digitalWrite(SPI_CS,HIGH); 
}
#define _spi_read SI4432Read
#define _spi_write SI4432Write
unsigned char ItStatus1, ItStatus2; 
static uint8 RF_Header[4] = {'O','L','R','S'}; 
#define RF22B_PWRSTATE_READY    01 
#define RF22B_PWRSTATE_TX        0x09 

#define RF22B_PWRSTATE_RX       05 
//-------Defaults 38.400 baud---------------------------------------------- 
void RF22B_init_parameter(void) 
{ 
 ItStatus1 = _spi_read(0x03); // read status, clear interrupt   
 ItStatus2 = _spi_read(0x04); 
  _spi_write(0x06, 0x00);    // no wakeup up, lbd, 
  _spi_write(0x07, RF22B_PWRSTATE_READY);      // disable lbd, wakeup timer, use internal 32768,xton = 1; in ready mode 
  _spi_write(0x09, 0x7f);  // c = 12.5p   
  _spi_write(0x0a, 0x05); 
  _spi_write(0x0b, 0x12);    // gpio0 TX State
  _spi_write(0x0c, 0x15);    // gpio1 RX State 
  //  -- Old PCB --
  //  _spi_write(0x0b, 0x15);    // gpio0 RX State
  //  _spi_write(0x0c, 0x12);    // gpio1 TX State 
  _spi_write(0x0d, 0xfd);    // gpio 2 micro-controller clk output 
  _spi_write(0x0e, 0x00);    // gpio    0, 1,2 NO OTHER FUNCTION. 
  
  _spi_write(0x70, 0x00);    // disable manchest 
  

   // 38.4Kbps data rate
  _spi_write(0x6e, 0x09); //case RATE_384K 
  _spi_write(0x6f, 0xD5); //case RATE_384K
  
  _spi_write(0x1c, 0x02); // case RATE_384K
  _spi_write(0x20, 0x68);//  0x20 calculate from the datasheet= 500*(1+2*down3_bypass)/(2^ndec*RB*(1+enmanch)) 
  _spi_write(0x21, 0x01); // 0x21 , rxosr[10--8] = 0; stalltr = (default), ccoff[19:16] = 0; 
  _spi_write(0x22, 0x3A); // 0x22    ncoff =5033 = 0x13a9 
  _spi_write(0x23, 0x93); // 0x23 
  _spi_write(0x24, 0x02); // 0x24 
  _spi_write(0x25, 0x6B); // 0x25 
  _spi_write(0x2a, 0x1e); 
  
  
  /*
  // 100Kbps data rate
  _spi_write(0x1c, 0x94); // case RATE_100K
  _spi_write(0x20, 0x3C);//  0x20 calculate from the datasheet= 500*(1+2*down3_bypass)/(2^ndec*RB*(1+enmanch)) 
  _spi_write(0x21, 0x02); // 0x21 , rxosr[10--8] = 0; stalltr = (default), ccoff[19:16] = 0; 
  _spi_write(0x22, 0x22); // 0x22    ncoff =5033 = 0x13a9 
  _spi_write(0x23, 0x22); // 0x23 
  _spi_write(0x24, 0x07); // 0x24 
  _spi_write(0x25, 0xFF); // 0x25 
  _spi_write(0x2a, 0x1e); 
  
  _spi_write(0x6e, 0x19); //case RATE_100K 
  _spi_write(0x6f, 0x9A); //case RATE_100K 
  */

  /*
  // 57.6Kbps data rate
  _spi_write(0x1c, 0x05); // case RATE_100K
  _spi_write(0x20, 0x45);//  0x20 calculate from the datasheet= 500*(1+2*down3_bypass)/(2^ndec*RB*(1+enmanch)) 
  _spi_write(0x21, 0x01); // 0x21 , rxosr[10--8] = 0; stalltr = (default), ccoff[19:16] = 0; 
  _spi_write(0x22, 0xD7); // 0x22    ncoff =5033 = 0x13a9 
  _spi_write(0x23, 0xDC); // 0x23 
  _spi_write(0x24, 0x05); // 0x24 
  _spi_write(0x25, 0x76); // 0x25 
  _spi_write(0x2a, 0x1e); 
  
  _spi_write(0x6e, 0x0E); //case RATE_100K 
  _spi_write(0x6f, 0xBF); //case RATE_100K 
  */
 

  _spi_write(0x30, 0x8c);    // enable packet handler, msb first, enable crc, 

  _spi_write(0x32, 0xf3);    // 0x32address enable for headere byte 0, 1,2,3, receive header check for byte 0, 1,2,3 
  _spi_write(0x33, 0x42);    // header 3, 2, 1,0 used for head length, fixed packet length, synchronize word length 3, 2, 
  _spi_write(0x34, 0x07);    // 7 default value or   // 64 nibble = 32byte preamble 
  _spi_write(0x36, 0x2d);    // synchronize word 
 _spi_write(0x37, 0xd4); 
 _spi_write(0x38, 0x00); 
 _spi_write(0x39, 0x00); 
 _spi_write(0x3a, RF_Header[0]);    // tx header 
 _spi_write(0x3b, RF_Header[1]); 
 _spi_write(0x3c, RF_Header[2]); 
 _spi_write(0x3d, RF_Header[3]); 
 _spi_write(0x3e, 6);    // total 6 byte Tx for telemetry
 
  
  
    //RX HEADER
 _spi_write(0x3f, RF_Header[0]);   // check hearder 
 _spi_write(0x40, RF_Header[1]); 
 _spi_write(0x41, RF_Header[2]); 
 _spi_write(0x42, RF_Header[3]); 
 _spi_write(0x43, 0xff);    // all the bit to be checked 
 _spi_write(0x44, 0xff);    // all the bit to be checked 
 _spi_write(0x45, 0xff);    // all the bit to be checked 
 _spi_write(0x46, 0xff);    // all the bit to be checked 
  

  
  _spi_write(0x6d, 0x07); // 7 set power max power 
  _spi_write(0x79, 0x00);    // no hopping 
  _spi_write(0x7a, 0x06);    // 60khz step size (10khz x value) // no hopping 

  _spi_write(0x71, 0x23); // Gfsk, fd[8] =0, no invert for Tx/Rx data, fifo mode, txclk -->gpio 
  _spi_write(0x72, 0x1F); // frequency deviation setting to 45k = 72*625 
  _spi_write(0x73, 0x00);   
  _spi_write(0x74, 0x00);    // no offset 
 

  //band 435.000
 
 _spi_write(0x75, 0x53);    
 _spi_write(0x76, 0x7D);    
 _spi_write(0x77, 0x00); 
}
